/*
 * SPDX-FileCopyrightText: 2022 Freie Universität Berlin
 * SPDX-License-Identifier: LGPL-2.1-only
 */

/**
 * @ingroup tests
 * @{
 * @file
 * @brief       FIDO2 CTAP test application that tests CTAP functionality
 *              without transport layer.
 *
 * @author      Nils Ollrogge <nils.ollrogge@mailbox.tu-dresden.de>
 * @}
 */
#include <stdio.h>
#include <stdlib.h>

#include "embUnit.h"

#include "fido2/ctap.h"

/**
 * To generate a new arrays simply run the gen_test_case.py script
 */
static uint8_t mc_data[] =
{ 0xa5, 0x1, 0x58, 0x20, 0xe0, 0xa1, 0xec, 0x5a, 0xa, 0x12, 0xa1, 0x4, 0xc8, 0xcb, 0x93, 0x54, 0x31,
  0xbf, 0x5c, 0x39, 0x7a, 0xee, 0x1b, 0x9f, 0xd0, 0x97, 0x97, 0x7d, 0x7b, 0xfb, 0x1, 0xa1, 0x20,
  0x2a, 0xad, 0x5c, 0x2, 0xa2, 0x62, 0x69, 0x64, 0x6b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
  0x2e, 0x63, 0x6f, 0x6d, 0x64, 0x6e, 0x61, 0x6d, 0x65, 0x6a, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c,
  0x65, 0x20, 0x52, 0x50, 0x3, 0xa2, 0x62, 0x69, 0x64, 0x47, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69,
  0x64, 0x64, 0x6e, 0x61, 0x6d, 0x65, 0x67, 0x41, 0x2e, 0x20, 0x55, 0x73, 0x65, 0x72, 0x4, 0x84,
  0xa2, 0x63, 0x61, 0x6c, 0x67, 0x26, 0x64, 0x74, 0x79, 0x70, 0x65, 0x6a, 0x70, 0x75, 0x62, 0x6c,
  0x69, 0x63, 0x2d, 0x6b, 0x65, 0x79, 0xa2, 0x63, 0x61, 0x6c, 0x67, 0x27, 0x64, 0x74, 0x79, 0x70,
  0x65, 0x6a, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2d, 0x6b, 0x65, 0x79, 0xa2, 0x63, 0x61, 0x6c,
  0x67, 0x38, 0x24, 0x64, 0x74, 0x79, 0x70, 0x65, 0x6a, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2d,
  0x6b, 0x65, 0x79, 0xa2, 0x63, 0x61, 0x6c, 0x67, 0x39, 0x1, 0x0, 0x64, 0x74, 0x79, 0x70, 0x65,
  0x6a, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2d, 0x6b, 0x65, 0x79, 0x7, 0xa1, 0x62, 0x72, 0x6b,
  0xf5, };

static uint8_t ga_data[] =
{ 0xa2, 0x1, 0x6b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2, 0x58,
  0x20, 0x7d, 0xf3, 0x9a, 0x49, 0xa9, 0x8c, 0xa2, 0xd8, 0x3e, 0x50, 0x36, 0x42, 0xd9, 0xc6, 0xfa,
  0x39, 0xf5, 0xa7, 0xe9, 0x35, 0x41, 0xb0, 0x1c, 0x59, 0xfa, 0xc2, 0x35, 0x3a, 0xb0, 0xbc, 0xcc,
  0x70, };

static void test_ctap(void)
{
    fido2_ctap_init();
    ctap_req_t req = { 0 };
    ctap_resp_t resp = { 0 };

    /* reset authenticator */
    req.method = CTAP_RESET;
    req.buf = NULL;
    req.len = 0x0;
    fido2_ctap_handle_request(&req, &resp);

    TEST_ASSERT(resp.status == CTAP2_OK);

    /* create new credential */
    req.method = CTAP_MAKE_CREDENTIAL;
    req.buf = mc_data;
    req.len = sizeof(mc_data);
    fido2_ctap_handle_request(&req, &resp);

    TEST_ASSERT(resp.status == CTAP2_OK);

    /* create assertion using credential */
    req.method = CTAP_GET_ASSERTION;
    req.buf = ga_data;
    req.len = sizeof(ga_data);
    fido2_ctap_handle_request(&req, &resp);

    TEST_ASSERT(resp.status == CTAP2_OK);
}

Test *ctap_tests(void)
{
    EMB_UNIT_TESTFIXTURES(fixtures) {
        new_TestFixture(test_ctap),
    };

    EMB_UNIT_TESTCALLER(ctap_tests, NULL, NULL, fixtures);

    return (Test *)&ctap_tests;
}

int main(void)
{
    TESTS_START();
    TESTS_RUN(ctap_tests());
    TESTS_END();

    return 0;
}
/** @} */
